﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using ChinaSoft.BLL;
using ChinaSoft.Model;

namespace Admin.Sys.InsuranceCategory
{
    public partial class InsuranceCategory_Edit : System.Web.UI.Page
    {
        private BLL_T_SYS_INSURANCECATEGORY bllInsranceCategory = new BLL_T_SYS_INSURANCECATEGORY();
        private Model_T_SYS_INSURANCECATEGORY modelInsranceCategory = new Model_T_SYS_INSURANCECATEGORY();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTreeView();
                ControlBtn(false, false, false, false, false);
                ControlTextBox(true, true);
            }
        }
        /// <summary>
        /// 控制控件的状态（禁用、启用）
        /// </summary>
        /// <param name="btnAdd"></param>
        /// <param name="btnDel"></param>
        /// <param name="btnUpdate"></param>
        /// <param name="btnSave"></param>
        /// <param name="btnCancel"></param>
        private void ControlBtn(bool btnAdd, bool btnDel, bool btnUpdate, bool btnSave, bool btnCancel)
        {
            imgBtnAdd.Enabled = btnAdd;
            imgBtnDel.Enabled = btnDel;
            imgBtnUpdate.Enabled = btnUpdate;
            imgBtnSave.Enabled = btnSave;
            imgBtnCancel.Enabled = btnCancel;
        }
        /// <summary>
        /// 绑定树性菜单
        /// </summary>
        private void BindTreeView()
        {
            TreeView1.Nodes.Clear();
            DataSet ds = bllInsranceCategory.GetAllList();
            ChinaSoft.BLL.Common.TreeViewOperate bllTreeView = new ChinaSoft.BLL.Common.TreeViewOperate();
            bllTreeView.BindTreeView(ds, TreeView1, "NAME", "INSURER_ID", false);
        }
        /// <summary>
        /// 当树形菜单选择改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            ControlBtn(true, true, true, false, false);
            string resourceId = ChinaSoft.Common.Input.SetString(TreeView1.SelectedNode.Value);
            txtName.Value = TreeView1.SelectedNode.Text;
            modelInsranceCategory = bllInsranceCategory.GetModel(resourceId);
            if (modelInsranceCategory != null)
            {
                txtName.Value = modelInsranceCategory.NAME;
            }

            ControlTextBox(true, false);
            HiddenInsurerId.Value = resourceId;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, false, true, true);
            ControlTextBox(false, true);
            HiddenInsurerId.Value = "";
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnDel_Click(object sender, ImageClickEventArgs e)
        {
            string roleId = ChinaSoft.Common.Input.SetString(TreeView1.SelectedNode.Value);
            bool result = bllInsranceCategory.Delete(roleId);
            if (result)
            {
                ChinaSoft.Common.MessageBox.Show(this, "删除成功！");
                ControlTextBox(false,true);
                BindTreeView();
                HiddenInsurerId.Value = "";
            }
            else
            {
                ChinaSoft.Common.MessageBox.Show(this, "该数据已经不存在");
                BindTreeView();
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnUpdate_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, false, true, true);
            ControlTextBox(false, false);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
        {
            if (HiddenInsurerId.Value == "")
            {
                #region 添加
                modelInsranceCategory.INSURANCECATEGORY_ID = Guid.NewGuid().ToString();
                modelInsranceCategory.NAME = txtName.Value.Trim();
                bllInsranceCategory.Add(modelInsranceCategory);
                #endregion
                ChinaSoft.Common.MessageBox.Show(this, "添加成功！");
                BindTreeView();
                ControlTextBox(true,true);
            }
            else
            {
                modelInsranceCategory = bllInsranceCategory.GetModel(HiddenInsurerId.Value);
                #region 更新
                modelInsranceCategory.NAME = txtName.Value.Trim();
                #endregion
                ChinaSoft.Common.MessageBox.Show(this, "更新成功！");
                BindTreeView();
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnCancel_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, true, false, false);
            ControlTextBox(true, true);
        }

        /// <summary>
        /// 控制文本框
        /// </summary>
        /// <param name="txtNameDisabled"></param>
        /// <param name="boolClear"></param>
        private void ControlTextBox(bool txtNameDisabled,bool boolClear)
        {
            if (boolClear)
            {
                txtName.Value = "";
            }
            txtName.Disabled = txtNameDisabled;
        }
    }
}